package com.example.ck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ck.dao.UserDao;
import com.example.ck.dto.UserDto;
import com.example.ck.entity.User;
import com.example.ck.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    /**
     * 判断用户名是否重名
     *
     * @param username
     * @return
     */
    @Override
    public boolean checkName(String username) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("username", username);
        User user = userDao.selectOne(qw);
        return user != null ? true : false;
    }

    /**
     * 注册用户
     *
     * @param user
     * @return
     */
    @Override
    public boolean registerUser(User user) {
        user.setCreatetime(LocalDateTime.now());
        user.setUpdatetime(LocalDateTime.now());
        int i = userDao.insert(user);
        return i > 0;
    }

    /**
     * 查询用户
     */
    @Override
    public UserDto queryUser(String username, String password, String type) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("username", username);
        qw.eq("password", password);
        qw.eq("type", type);
        User user = userDao.selectOne(qw);
        if (user != null) {
            return new UserDto(user.getHeader(), user.getId(), user.getUsername(), user.getType(), UUID.randomUUID().toString());
        } else {
            return null;
        }
    }

    /**
     * 根据用户名获取用户所有信息
     */
    @Override
    public User findUser(String username) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("username", username);
        User user = userDao.selectOne(qw);
        return user;
    }

    /**
     * 根据用户id查询用户
     *
     * @param id
     * @return
     */
    @Override
    public User findUserByid(Integer id) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", id);
        User user = userDao.selectOne(qw);
        return user;
    }

    /**
     * 用户根据id修改自身的信息
     */
    @Override
    public boolean updateUserInfo(Integer id, String header, String username, String professional,
                                  String password, String email, String phone) {
        User user = findUserByid(id);

        user.setHeader(header);
        user.setUsername(username);
        user.setProfessional(professional);
        user.setPassword(password);
        user.setEmail(email);
        user.setPhone(phone);
        user.setUpdatetime(LocalDateTime.now());

        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", id);

        int i = userDao.update(user, qw);
        return i > 0;
    }

    /**
     * 管理员查询所有用户
     *
     * @return
     */
    @Override
    public List<User> queryAllUser() {
        List<User> users = userDao.selectList(null);
        return users;
    }

    /**
     * 管理员根据id移除用户
     */
    @Override
    public boolean removeUserById(Integer id) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", id);
        int i = userDao.delete(qw);
        return i > 0;
    }

    /**
     * 管理员根据用户名修改用户信息
     */
    @Override
    public boolean updateUserbyId(Integer id, String email, String phone, String professional, String type) {
        User user = findUserByid(id);

        user.setEmail(email);
        user.setPhone(phone);
        user.setProfessional(professional);
        user.setType(type);
        user.setUpdatetime(LocalDateTime.now());

        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", id);
        int i = userDao.update(user, qw);
        return i > 0;
    }

    /**
     * 获取所有类型为专家的用户
     * @return
     */
    @Override
    public List<User> queryAllExpert() {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("type", "专家");
        List<User> users = userDao.selectList(qw);
        return users;
    }


}
